package com.xbongbong.saas.script.tempenum;

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.PatternEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.paas.field.SubFormPoJo;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.PhoneExtEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContactEnum;
import com.xbongbong.saas.enums.dictionary.ContactLevelEnum;
import com.xbongbong.saas.enums.dictionary.RelationshipEnum;
import com.xbongbong.saas.enums.dictionary.SexEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.sys.enums.RoleEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 联系人刷脚本枚举
 *
 * @author feng.zheng
 * @version v1.0
 * @date 2019/1/15 15:42
 * @since v1.0
 */
@Getter
public enum TempContactEnum {
    //刷脚本专用枚举
    BASE_INFO(ContactEnum.BASE_INFO),
    AVATAR(ContactEnum.AVATAR),
    NAME(ContactEnum.NAME),
    PHONE(ContactEnum.PHONE),
    CUSTOMER_NAME(ContactEnum.CUSTOMER_NAME),
    EMAIL(ContactEnum.EMAIL),
    QQ(ContactEnum.QQ),
    ADDRESS(ContactEnum.ADDRESS),
    ZIP_CODE(ContactEnum.ZIP_CODE),
    SEX(ContactEnum.SEX),
    BIRTHDAY(ContactEnum.BIRTHDAY),
    HOBBIES(ContactEnum.HOBBIES),
    MEMO(ContactEnum.MEMO),

    EXTEND_INFO(ContactEnum.EXTEND_INFO),
    DEPARTMENT(ContactEnum.DEPARTMENT),
    POSITION(ContactEnum.POSITION),
    LEVEL(ContactEnum.LEVEL),
    IMPORTANT_DEGREE(ContactEnum.IMPORTANT_DEGREE),
    RELATIONSHIP(ContactEnum.RELATIONSHIP),
    INTIMATE_DEGREE(ContactEnum.INTIMATE_DEGREE),

    SYSTEM_INFO(ContactEnum.SYSTEM_INFO),
    CREATOR_ID(ContactEnum.CREATOR_ID),
    ADD_TIME(ContactEnum.ADD_TIME),
    UPDATE_TIME(ContactEnum.UPDATE_TIME),

    CUSTOMER_NAME_LINK_TEXT(ContactEnum.CUSTOMER_NAME_LINK_TEXT),
    BIRTHDAY_MMDD(ContactEnum.BIRTHDAY_MMDD),
    DD_CONTACT_ID(ContactEnum.DD_CONTACT_ID),
    BUSINESS_CARD(ContactEnum.BUSINESS_CARD),
    IS_MAIN(ContactEnum.IS_MAIN),
    BIRTHDAY_FLAG(ContactEnum.BIRTHDAY_FLAG),
    OWNER_ID(ContactEnum.OWNER_ID),
    COUSERID(ContactEnum.COUSERID),
    ;
    /**
     * 联系人枚举
     */
    private ContactEnum anEnum;

    TempContactEnum(ContactEnum anEnum) {
        this.anEnum = anEnum;
    }

    public static List<TempContactEnum> getAllEnum() {
        List<TempContactEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }

    public static void initAvatar(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setMaxFileCount(BasicConstant.ONE);
        fieldAttrEntity.setOnlyOneFile(BasicConstant.ONE);
        fieldAttrEntity.setOnlyOneFileForbidden(BasicConstant.ONE);
        fieldAttrEntity.setAutoCompress(BasicConstant.ONE);
    }

    public static void initName(FieldAttrEntity fieldAttrEntity){
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.CONTACT.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.CONTACT.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(NAME.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化联系电话
     *
     * @param fieldAttrEntity 字段解释实体
     */
    public static void initPhone(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setNoRepeat(0);
        SubFormPoJo subFormPoJo = new SubFormPoJo();
        List<FieldAttrEntity> items = new ArrayList<>();
        FieldAttrEntity item = InitExplainsHelper.setField("text_1", "类型", "type", FieldTypeEnum.COMBO.getType(), 4, ShowTypeEnum.ALL.getCode(), 0, 1);
//        String str = "工作,手机,住址,主要,传真,其他";
        List<ItemPoJo> itemPoJoList = InitExplainsHelper.setCombItemByEnum(PhoneExtEnum.class, FieldTypeEnum.COMBO.getType());
        itemPoJoList.get(0).setChecked(true);
        item.setItems(itemPoJoList);
        item.setRequired(1);
        item.setParentAttr(PHONE.anEnum.getAttr());
        item.setDictionaryFlag(1);
        items.add(item);
        FieldAttrEntity item2 = InitExplainsHelper.setField("text_2", "电话", "phone", FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.ALL.getCode(), 0, 1);
        item2.setRequired(1);
        item2.setParentAttr(PHONE.anEnum.getAttr());
        items.add(item2);
        subFormPoJo.setItems(items);
        fieldAttrEntity.setSubForm(subFormPoJo);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType("custom");
        defaultAttrPoJo.setDefaultValue("");
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setRequired(1);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * CRM简化版-初始化联系电话
     *
     * @param fieldAttrEntity 字段解释实体
     * @param phoneArray 表单字段镜像
     */
    public static void initPhone(FieldAttrEntity fieldAttrEntity, JSONArray phoneArray) {
        fieldAttrEntity.setNoRepeat(0);
        SubFormPoJo subFormPoJo = new SubFormPoJo();
        List<FieldAttrEntity> items = new ArrayList<>();
        if (phoneArray.contains("subForm_1.text_1")) {
            FieldAttrEntity item = InitExplainsHelper.setField("text_1", "类型", "type", FieldTypeEnum.COMBO.getType(), 4, ShowTypeEnum.ALL.getCode(), 0, 1);
            List<ItemPoJo> itemPoJoList = InitExplainsHelper.setCombItemByEnum(PhoneExtEnum.class, FieldTypeEnum.COMBO.getType());
            itemPoJoList.get(0).setChecked(true);
            item.setItems(itemPoJoList);
            item.setRequired(1);
            item.setParentAttr(PHONE.anEnum.getAttr());
            item.setDictionaryFlag(1);
            items.add(item);
        }
        if (phoneArray.contains("subForm_1.text_2")) {
            FieldAttrEntity item2 = InitExplainsHelper.setField("text_2", "电话", "phone", FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.ALL.getCode(), 0, 1);
            item2.setRequired(1);
            item2.setParentAttr(PHONE.anEnum.getAttr());
            items.add(item2);
            subFormPoJo.setItems(items);
        }
        fieldAttrEntity.setSubForm(subFormPoJo);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType("custom");
        defaultAttrPoJo.setDefaultValue("");
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setRequired(1);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化关联客户
     *
     * @param fieldAttrEntity 字段解释实体
     */
    public static void initCustomerName(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.CUSTOMER.getCode());
        fieldAttrEntity.setLinkedText(CUSTOMER_NAME_LINK_TEXT.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化联系人级别
     *
     * @param fieldAttrEntity 字段解释实体
     */
    public static void initLevel(FieldAttrEntity fieldAttrEntity) {
//        String str = "基层,中层,高层";
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(ContactLevelEnum.class, fieldAttrEntity.getFieldType()));
        fieldAttrEntity.setDictionaryFlag(1);
    }

    /**
     * 初始化性别
     *
     * @param fieldAttrEntity 字段解释实体
     */
    public static void initSex(FieldAttrEntity fieldAttrEntity) {
//        String str = "男,女";
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(SexEnum.class, fieldAttrEntity.getFieldType()));
        fieldAttrEntity.setDictionaryFlag(1);
    }


    /**
     * 初始化决策关系
     *
     * @param fieldAttrEntity 字段解释实体
     */
    public static void initRelationship(FieldAttrEntity fieldAttrEntity) {
//        String str = "关键决策人,分项决策人,商务决策人,技术决策人,财务决策人,使用人,意见影响人,普通人";
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(RelationshipEnum.class, fieldAttrEntity.getFieldType()));
        fieldAttrEntity.setDictionaryFlag(1);
    }

    /**
     * 初始化是否主联系人
     *
     * @param fieldAttrEntity 字段解释实体
     * @author feng.zheng
     * @date 2019/1/21 20:16
     */
    public static void initIsMain(FieldAttrEntity fieldAttrEntity) {
        String str = "是,否";
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItem(str));
    }

    /**
     * 初始化邮政编码
     *
     * @param fieldAttrEntity 字段解释实体
     * @author feng.zheng
     * @date 2019/1/29 21:11
     */
    public static void initZipCode(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setPatternType(PatternEnum.POSTCODE.getType());
        fieldAttrEntity.setVerifyRule(PatternEnum.POSTCODE.getPattern());
        fieldAttrEntity.setVerifyMessage(PatternEnum.POSTCODE.getMsg());
    }

    /**
     * 初始化邮箱
     *
     * @param fieldAttrEntity 字段解释实体
     * @author feng.zheng
     * @date 2019/1/29 21:13
     */
    public static void initEmail(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setPatternType(PatternEnum.EMAIL.getType());
        fieldAttrEntity.setVerifyRule(PatternEnum.EMAIL.getPattern());
        fieldAttrEntity.setVerifyMessage(PatternEnum.EMAIL.getMsg());
    }
    /**
     * 初始化生日
     *
     * @param fieldAttrEntity 实体
     * @author feng.zheng
     * @date 2019/2/14 15:53
     */
    public static void initBirthday(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedText(BIRTHDAY_FLAG.anEnum.getAttr());
        fieldAttrEntity.setDateType(DateTimeEnum.TIME.getType());
    }
    public static void initCreatorId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initAddTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initUpdateTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initOwnerId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setShowType(ShowTypeEnum.ALL.getCode());
        fieldAttrEntity.setEditHide(0);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setRequired(1);
        //可新增权限：默认【超级管理员、销售主管、创建人、负责人、负责人主管】可新增负责人，支持自定义；
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        //可删除权限：默认【超级管理员、销售主管】可删除负责人，支持自定义；
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setMultiple(1);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
    }

    /**
     * 初始化协同人字段
     *
     * @param fieldAttrEntity
     */
    public static void initCoUserId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setShowType(ShowTypeEnum.ALL.getCode());
        fieldAttrEntity.setFormPermission(BasicConstant.ONE);
        fieldAttrEntity.setIsForbidden(BasicConstant.ZERO);
        fieldAttrEntity.setEditable(1);
        //可新增权限：超级管理员、老板、销售主管、创建人、负责人、负责人主管
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setSetType(3);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
    }

}
